package com.deep.sentinel.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.deep.sentinel.common.CommonResult;
import com.deep.sentinel.entity.Dept;
import com.deep.sentinel.service.DeptService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Value("${server.port}")
    private String serverPort;

    @ResponseBody
    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @SentinelResource(value = "default")
    public CommonResult<Dept> get(@PathVariable("id") int id) {
        log.info("端口：" + serverPort + "\t+ dept/get/");
        try {
            TimeUnit.SECONDS.sleep(1);
            log.info("休眠 1秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Dept dept = deptService.get(id);
        System.out.println(dept);
        CommonResult<Dept> result = new CommonResult(200, "from mysql,serverPort:  " + serverPort, dept);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public CommonResult<List<Dept>> list() {
        log.info("端口：" + serverPort + "\t+ dept/list/");

        List<Dept> depts = deptService.selectAll();
        CommonResult<List<Dept>> result = new CommonResult(200, "from mysql,serverPort:  " + serverPort, depts);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/dept", method = RequestMethod.GET)
    @SentinelResource(value = "default")
    public CommonResult<List<Dept>> dept() {
        log.info("端口：" + serverPort + "\t+ dept/");

        List<Dept> depts = deptService.selectAll();
        CommonResult<List<Dept>> result = new CommonResult(200, "from mysql,serverPort:  " + serverPort, depts);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    @SentinelResource(value = "default")
    public CommonResult<String> demo() {
        log.info("端口：" + serverPort + "\t+ demo/");
        CommonResult<String> result = new CommonResult(200, "from mysql,serverPort:  " + serverPort, "demo");
        return result;
    }
}